package com.jiangyg.mall.core.support.validation;

import org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorContextImpl;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.lang.annotation.Annotation;

/**
 * 类描述：校验执行抽象类
 * 
 * @author jiangyg
 * @date 2020-08-02
 * @version 2.0
 */
@SuppressWarnings("unchecked")
public abstract class AbstractConstraintValidator<A extends Annotation, T> implements ConstraintValidator<A, T> {

	@Override
	public boolean isValid(T value, ConstraintValidatorContext context) {
		ConstraintValidatorContextImpl contextImpl = (ConstraintValidatorContextImpl) context;
		A a = (A) contextImpl.getConstraintDescriptor().getAnnotation();
		return this.execute(a, value, contextImpl);
	}
	
	@Override
	public void initialize(A constraintAnnotation) {
		
	}

	/**
	 * 功能描述：执行校验
	 * 
	 * @param a           注解
	 * @param value       属性值
	 * @param contextImpl 校验上下文
	 * @return 返回boolean，校验是否成功
	 */
	public abstract boolean execute(A a, T value, ConstraintValidatorContextImpl contextImpl);

}
